﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using Auction_Server.Business;
using WaterKit.Base;

namespace Auction_Server
{
    /// <summary>
    /// ConfirmationWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ConfirmationWindow : Window
    {
        public ConfirmationWindow(targetModel model, bool currentBid = true)
        {
            //标的下单位
            DataTable dtCompany = new DataTable();

            InitializeComponent();

            #region 标的信息绘制 控件:gdTargetInfo

            //标的编号
            Label lblTargetName_1 = new Label();
            lblTargetName_1.Content = "标的编号";
            lblTargetName_1.HorizontalContentAlignment = HorizontalAlignment.Center;
            gdTargetInfo.Children.Add(lblTargetName_1);
            Label TargetName_1 = new Label();
            TargetName_1.SetValue(Grid.ColumnProperty, 1);
            gdTargetInfo.Children.Add(TargetName_1);

            //标的名称
            Label lblTargetNo = new Label();
            lblTargetNo.Content = "标的名称";
            lblTargetNo.HorizontalContentAlignment = HorizontalAlignment.Center;
            lblTargetNo.SetValue(Grid.ColumnProperty, 2);
            gdTargetInfo.Children.Add(lblTargetNo);
            Label lblTargetNo_1 = new Label();
            lblTargetNo_1.SetValue(Grid.ColumnProperty, 3);
            gdTargetInfo.Children.Add(lblTargetNo_1);

            //标的数量
            Label lblTargetNum_1 = new Label();
            lblTargetNum_1.Content = "标的数量";
            lblTargetNum_1.HorizontalContentAlignment = HorizontalAlignment.Center;
            lblTargetNum_1.SetValue(Grid.RowProperty, 1);
            gdTargetInfo.Children.Add(lblTargetNum_1);

            Label lblTargetNum = new Label();
            lblTargetNum.SetValue(Grid.ColumnProperty, 1);
            lblTargetNum.SetValue(Grid.RowProperty, 1);
            gdTargetInfo.Children.Add(lblTargetNum);

            //标的底价
            Label lblTargetMoney = new Label();
            lblTargetMoney.Content = "标的底价";
            lblTargetMoney.HorizontalContentAlignment = HorizontalAlignment.Center;
            lblTargetMoney.SetValue(Grid.ColumnProperty, 2);
            lblTargetMoney.SetValue(Grid.RowProperty, 1);
            gdTargetInfo.Children.Add(lblTargetMoney);
            Label lblTargetMoney_1 = new Label();
            lblTargetMoney_1.SetValue(Grid.ColumnProperty, 3);
            lblTargetMoney_1.SetValue(Grid.RowProperty, 1);
            gdTargetInfo.Children.Add(lblTargetMoney_1);

            //竞拍时间
            Label lblTargetTime = new Label();
            lblTargetTime.Content = "竞拍时间";
            lblTargetTime.HorizontalContentAlignment = HorizontalAlignment.Center;
            lblTargetTime.SetValue(Grid.RowProperty, 3);
            gdTargetInfo.Children.Add(lblTargetTime);
            Label lblTargetTime_1 = new Label();
            lblTargetTime_1.SetValue(Grid.ColumnProperty, 1);
            lblTargetTime_1.SetValue(Grid.RowProperty, 2);
            gdTargetInfo.Children.Add(lblTargetTime_1);

            //竞拍地点
            Label lblTargetAddr = new Label();
            lblTargetAddr.Content = "竞拍地点";
            lblTargetAddr.HorizontalContentAlignment = HorizontalAlignment.Center;
            lblTargetAddr.SetValue(Grid.ColumnProperty, 2);
            lblTargetAddr.SetValue(Grid.RowProperty, 3);
            gdTargetInfo.Children.Add(lblTargetAddr);
            Label lblTargetAddr_1 = new Label();
            lblTargetAddr_1.SetValue(Grid.ColumnProperty, 3);
            lblTargetAddr_1.SetValue(Grid.RowProperty, 2);
            gdTargetInfo.Children.Add(lblTargetAddr_1);

            #endregion

            #region 竞拍各方的最终报价绘制 控件：gdTargetList

            //排序
            Label lblSort = new Label();
            lblSort.Content = "排序";
            lblSort.HorizontalContentAlignment = HorizontalAlignment.Center;
            gdTargetList.Children.Add(lblSort);

            //报价
            Label lblPrice = new Label();
            lblPrice.Content = "报价";
            lblPrice.HorizontalContentAlignment = HorizontalAlignment.Center;
            lblPrice.SetValue(Grid.ColumnProperty, 1);
            gdTargetList.Children.Add(lblPrice);

            //报价人座位号
            Label lblSeatNumber = new Label();
            lblSeatNumber.Content = "报价人座位号";
            lblSeatNumber.HorizontalContentAlignment = HorizontalAlignment.Center;
            lblSeatNumber.SetValue(Grid.ColumnProperty, 2);
            gdTargetList.Children.Add(lblSeatNumber);

            //报价时间
            Label lblTime = new Label();
            lblTime.Content = "报价时间";
            lblTime.HorizontalContentAlignment = HorizontalAlignment.Center;
            lblTime.SetValue(Grid.ColumnProperty, 3);
            gdTargetList.Children.Add(lblTime);

            #endregion

            #region 本标的中标人绘制 控件：gdTargetBid

            //座位号
            Label lblseatNumber = new Label();
            lblseatNumber.Content = "座位号";
            lblseatNumber.HorizontalContentAlignment = HorizontalAlignment.Center;
            gdTargetBid.Children.Add(lblseatNumber);
            Label seatNumber = new Label();
            seatNumber.HorizontalContentAlignment = HorizontalAlignment.Center;

            seatNumber.SetValue(Grid.RowProperty, 1);
            gdTargetBid.Children.Add(seatNumber);

            //中标单位
            Label lblcompanyName = new Label();
            lblcompanyName.Content = "中标单位";
            lblcompanyName.HorizontalContentAlignment = HorizontalAlignment.Center;
            lblcompanyName.SetValue(Grid.ColumnProperty, 1);
            gdTargetBid.Children.Add(lblcompanyName);
            Label companyName = new Label();
            companyName.HorizontalContentAlignment = HorizontalAlignment.Center;
            companyName.SetValue(Grid.ColumnProperty, 1);
            companyName.SetValue(Grid.RowProperty, 1);
            gdTargetBid.Children.Add(companyName);

            //数量
            Label lblTargetName = new Label();
            lblTargetName.Content = "数量";
            lblTargetName.HorizontalContentAlignment = HorizontalAlignment.Center;
            lblTargetName.SetValue(Grid.ColumnProperty, 2);
            gdTargetBid.Children.Add(lblTargetName);
            Label targetName = new Label();
            targetName.HorizontalContentAlignment = HorizontalAlignment.Center;
            targetName.SetValue(Grid.ColumnProperty, 2);
            targetName.SetValue(Grid.RowProperty, 1);
            gdTargetBid.Children.Add(targetName);

            //中报价
            Label lblmaxMoney = new Label();
            lblmaxMoney.Content = "中标价";
            lblmaxMoney.HorizontalContentAlignment = HorizontalAlignment.Center;
            lblmaxMoney.SetValue(Grid.ColumnProperty, 3);
            gdTargetBid.Children.Add(lblmaxMoney);
            Label maxMoney = new Label();
            maxMoney.HorizontalContentAlignment = HorizontalAlignment.Center;
            maxMoney.SetValue(Grid.ColumnProperty, 3);
            maxMoney.SetValue(Grid.RowProperty, 1);
            gdTargetBid.Children.Add(maxMoney);

            #endregion

            #region 输入标的信息

            targetService tarS = new targetService();
            dtCompany = tarS.getTargetCompany(model.Target_NO);

            DataRow drUnit = null;

            //是否为当前竞拍标的
            if (currentBid)
            {
                drUnit = dtCompany.Select("BIDUSER_NO='" + App.maxMoneyBidUser + "'")[0];
            }
            else
            {
                drUnit = dtCompany.Select("BIDUSER_NO='" + model.Biduser + "'")[0];
            }

            //标的编号
            TargetName_1.Content = model.Target_NO;
            //标的名称
            lblTargetNo_1.Content = model.Target_NAME;
            //交易底价
            lblTargetMoney_1.Content = model.Product_PRICE;
            //竞拍时间
            lblTargetTime_1.Content = DateTime.Now.ToShortDateString();
            //竞拍地点
            lblTargetAddr_1.Content = App.todayAuction.Rows[0]["auction_add"];
            //数量
            targetName.Content = model.Product_AUCTION_METE + App.htBidCodeToName[model.Product_MEASUREMENT];
            //标的数量
            lblTargetNum.Content = model.Product_AUCTION_METE + App.htBidCodeToName[model.Product_MEASUREMENT];
            //竞拍单位
            companyName.Content = drUnit["COMPANY_NAME"];
            //座位号
            seatNumber.Content = drUnit["SEATNUMBER"];
            //最终报价
            maxMoney.Content = App.userTargetStr == "" ? model.Bidprice : App.maxMoney;

            //项目附属
            expand_info.Content = AppOpr.GetConfigStrings("companyName") + "      时间：" + DateTime.Now.ToShortDateString();

            baseDataService baseService = new baseDataService();
            DataTable bid_price_dt = baseService.getTargetConfirmation(model.Target_NO);

            // 竞拍各方的最终报价赋值
            for (int i = bid_price_dt.Rows.Count-1; i >=0; i--)
            {
                DataRow dr = dtCompany.Select("BIDUSER_NO='" + bid_price_dt.Rows[i]["BIDUSER_NO"].ToString() + "'")[0];

                RowDefinition row = new RowDefinition();
                gdTargetList.RowDefinitions.Add(row);

                //排序
                Label Sort = new Label();
                Sort.Content = i.ToString();
                Sort.HorizontalContentAlignment = HorizontalAlignment.Center;
                Sort.SetValue(Grid.ColumnProperty, 0);
                Sort.SetValue(Grid.RowProperty, i + 1);
                gdTargetList.Children.Add(Sort);

                //报价
                Label Price = new Label();
                Price.Content = bid_price_dt.Rows[i]["PRICE"].ToString();
                Price.HorizontalContentAlignment = HorizontalAlignment.Center;
                Price.SetValue(Grid.ColumnProperty, 1);
                Price.SetValue(Grid.RowProperty, i + 1);
                gdTargetList.Children.Add(Price);

                //报价人席位
                Label seatNumber_2 = new Label();
                seatNumber_2.Content = dr["SEATNUMBER"].ToString();
                seatNumber_2.HorizontalContentAlignment = HorizontalAlignment.Center;
                seatNumber_2.SetValue(Grid.ColumnProperty, 2);
                seatNumber_2.SetValue(Grid.RowProperty, i + 1);
                gdTargetList.Children.Add(seatNumber_2);

                //报价时间
                Label Time_1 = new Label();
                Time_1.Content = bid_price_dt.Rows[i]["BIDTIME"].ToString();
                Time_1.HorizontalContentAlignment = HorizontalAlignment.Center;
                Time_1.SetValue(Grid.ColumnProperty, 3);
                Time_1.SetValue(Grid.RowProperty, i + 1);
                gdTargetList.Children.Add(Time_1);
            }

            // 绘制边框
            var gdTargetBid_rows = gdTargetList.RowDefinitions.Count;
            var gdTargetBid_columns = gdTargetList.ColumnDefinitions.Count;

            for (int i = 0; i < gdTargetBid_rows; i++)
            {
                for (int j = 0; j < gdTargetBid_columns; j++)
                {
                    var border = new Border() { BorderBrush = new SolidColorBrush(Colors.Gray), BorderThickness = new Thickness(1) };
                    Grid.SetRow(border, i);
                    Grid.SetColumn(border, j);
                    gdTargetList.Children.Add(border);
                }
            }

            #endregion
        }
    }
}
